﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 自动化设备状态机
{
    public partial class Form1 : Form
    {
        private StationBase _station;

        public Form1()
        {
            InitializeComponent();
        }
        private void uiListBox1_DoubleClick(object sender, EventArgs e)
        {

        }

        private async void btnRun_Click(object sender, EventArgs e)
        {
            btnRun.Enabled = false;
            var ret = await _station.Run(100, 5000);
            Showmsg("返回结果： " + ret);
            btnRun.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _station = new StationBase();
            EventMgrLib.EventMgr.Ins.GetEvent<StationMessage>().Subscribe(Showmsg);
        }

        private void Showmsg(string obj)
        {
            BeginInvoke(new Action(() => uiListBox1.Items.Add(obj)));
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            //模拟暂停按钮按下
            _station.Trig(StationBase.Trigger.Pause);
        }


        private void btnResume_Click(object sender, EventArgs e)
        {
            //模拟继续按钮按下

            _station.Trig(StationBase.Trigger.Resume);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            //模拟用户停止，或内部发出停止指令
            _station.Trig(StationBase.Trigger.Stop);
        }

        private void btnError_Click(object sender, EventArgs e)
        {
            //轴报警，光栅，急停，安全门
            _station.Trig(StationBase.Trigger.Alarm);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //清除报警，重新使能
            _station.Trig(StationBase.Trigger.ClearAlarm);
        }
        private void btnRunSuccess_Click(object sender, EventArgs e)
        {
            //模拟运动成功
            _station.Trig(StationBase.Trigger.RunOk);
        }
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uiListBox1.Items.Clear();
        }

        private void btnEmgStop_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.EmgStop);
        }
     
        private void uiButton1_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.Initlize);
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.Reset);
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.Home);
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.HomeOK);
        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.HomeFailed);
        }
        private void uiButton6_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.TakeAwayProduct);
        }

        private void uiButton7_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.StartJog);
        }

        private void uiButton8_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.StartCalibrate);
        }

        private void uiButton9_Click(object sender, EventArgs e)
        {
            _station.Trig(StationBase.Trigger.StartBurnin);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_station.Trig(StationBase.Trigger.Close))
            {
                e.Cancel = true;
            }
        }

        private void uiButton10_Click(object sender, EventArgs e)
        {
            // 3轴绝对运动

            var parm = new MoveParam(new List<int>() { 0, 2, 3 },
                new List<double>() { 100, -100, 300 },
                new Speed() { Velocity = 50, Acc = 500, Dec = 500 });
            //Step1,外部调用触发
            _station.Trig(_station.RunTrigger, parm);
        }


        private void uiButton11_Click(object sender, EventArgs e)
        {
            //3轴相对插补


            var parm = new MoveParam(new List<int>() { 0, 2, 3 },
                new List<double>() { 5, -5, 5 },
                new Speed() { Velocity = 50, Acc = 500, Dec = 500 }, true, true);
            //Step1,外部调用触发
            _station.Trig(_station.RunTrigger, parm);

        }
        private void uiButton12_Click(object sender, EventArgs e)
        {
            //双控气缸伸出
            var parm = new CylinderParam(new List<int>() { 1, 2 }, new List<bool>() { true, false },
                new List<int>() { 7, 8 }, new List<bool>() { true, false });
            //Step1,外部调用触发
            _station.Trig(_station.RunTrigger, parm);
        }

        private void uiButton13_Click(object sender, EventArgs e)
        {
            //双控气缸缩回
            var parm = new CylinderParam(new List<int>() { 1, 2 }, new List<bool>() { false, true },
                new List<int>() { 7, 8 }, new List<bool>() { false, true });
            //Step1,外部调用触发
            _station.Trig(_station.RunTrigger, parm);
        }


        private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.uiListBox1.Items.Clear();
        }
    }
}
